package com.site.blog.my.core.service.impl;

import com.site.blog.my.core.dao.BlogTagMapper;
import com.site.blog.my.core.dao.BlogTagRelationMapper;
import com.site.blog.my.core.entity.BlogTag;
import com.site.blog.my.core.service.TagService;
import com.site.blog.my.core.util.PageQueryUtil;
import com.site.blog.my.core.util.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: my-blog
 * @description:
 * @author: zxf
 * @create: 2021-07-01 10:11
 **/
@Service
public class TagServiceImpl implements TagService {

    @Resource
    private BlogTagMapper blogTagMapper;
    @Resource
    private BlogTagRelationMapper relationMapper;

    @Override
    public PageResult getBlogTagPage(PageQueryUtil pageUtil) {
        List<BlogTag> tagList = blogTagMapper.findTagList(pageUtil);
        // 查出标签总数
        int total = blogTagMapper.getTotalTags(pageUtil);
        PageResult pageResult = new PageResult(tagList,total, pageUtil.getPage(), pageUtil.getLimit());
        return pageResult;
    }

    @Override
    public Boolean saveTag(String tagName) {
        BlogTag temp = blogTagMapper.selectByTagName(tagName);
        if (temp == null){
            BlogTag blogTag = new BlogTag();
            blogTag.setTagName(tagName);
            return blogTagMapper.insertSelective(blogTag) > 0;
        }
        return false;
    }

    @Override
    public Boolean deleteBatch(Integer[] ids) {

        List<Long> list = relationMapper.selectDistinctTagIds(ids);
        // 有关联数据，不能删除
        if (list != null && list.size() > 0) return false;
        return blogTagMapper.deleteBatch(ids) > 0;
    }
}

